{% load trans from i18n %}
{% load django_ledger %}

<form action="{% url 'django_ledger:invoice-update-items' entity_slug=entity_slug invoice_pk=invoice_model.uuid %}"
      method="post">
    <div class="columns is-multiline">
        <div class="column is-12">
            <h1 class="is-size-1">{% trans 'Invoice Items' %}</h1>
        </div>
        <div class="column is-12">
            <div class="table-container">
                {% csrf_token %}
                {{ itemtxs_formset.non_form_errors }}
                {{ itemtxs_formset.management_form }}
                <table class="table is-fullwidth is-narrow is-striped is-bordered">
                    <thead>
                    <tr>
                        <th>{% trans 'Item' %}</th>
                        <th>{% trans 'Quantity' %}</th>
                        <th>{% trans 'Unit Cost' %}</th>
                        <th>{% trans 'Available' %}</th>
                        <th>{% trans 'Total' %}</th>
                        <th>{% trans 'Delete' %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for f in itemtxs_formset %}
                        <tr>
                            <td>
                                {% for hidden_field in f.hidden_fields %}
                                    {{ hidden_field }}
                                {% endfor %}
                                {{ f.item_model }}
                                {% if f.errors %}
                                    {{ f.errors }}
                                {% endif %}
                            </td>
                            <td id="{{ f.instance.html_id_quantity }}">{{ f.quantity }}</td>
                            <td id="{{ f.instance.html_id_unit_cost }}">{{ f.unit_cost }}</td>
                            <td>{% if f.instance.item_model.for_inventory and not f.instance.invoice_model.is_approved %}
                                {{ f.instance.item_model.inventory_received }}
                            {% endif %}</td>
                            <td class="has-text-right" id="{{ f.instance.html_id_total_amount }}">
                                {% currency_symbol %}{{ f.instance.total_amount | currency_format }}</td>
                            <td class="has-text-centered">
                                {% if itemtxs_formset.can_delete %}
                                    {{ f.DELETE }}
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                    <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="has-text-right">{% trans 'Total' %}</th>
                        <th class="has-text-right">{% currency_symbol %}{{ total_amount__sum | currency_format }}</th>
                        <th></th>
                    </tr>
                    </tfoot>

                </table>
            </div>
        </div>
        <div class="column is-12">
            <a href="{% url 'django_ledger:product-create' entity_slug=entity_slug %}"
               class="button is-primary">{% trans 'New Item' %}</a>
            <button class="button is-primary">{% trans 'Save' %}</button>
        </div>
    </div>
</form>